package org.exes;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class FlujosCaracter {

    public static void main(String[] args) throws IOException {
        if(false){
            Writer w = new FileWriter("c:\\salida.txt");
            w.write("Prueba\r\n");
            w.append("Linea 2").append("\r\n");
            w.flush();
            w.close();
            Reader r = new FileReader("c:\\salida.txt");
            
            BufferedReader br = new BufferedReader(r);
            String linea = br.readLine();
            System.out.println(linea);
            linea = br.readLine();
            System.out.println(linea);
        }
        if(false){
           OutputStream os = new FileOutputStream("c:\\salida2.txt");
           
/*           os.write('a');
           os.write("aСловариbc".getBytes());
           os.write("aСловариbc".getBytes("UTF-16"));
*/
           DataOutputStream dos = new DataOutputStream(os);
           dos.writeFloat(12);
           dos.writeInt(4567);
           dos.writeBytes("cadena");
           dos.writeChars("cadena");
           dos.writeUTF("cadena");
           dos.flush();
           dos.close();
           
           InputStream fis = new FileInputStream("c:\\salida2.txt");
           DataInputStream dis = new DataInputStream(fis);
           float f = dis.readFloat();
           int i = dis.readInt();
           System.out.println(f+" "+i);
           byte [] buffer = new byte[6];
           dis.read(buffer);
           String cad = new String(buffer,"ASCII");
           System.out.println(cad);

           buffer = new byte[12];
           dis.read(buffer);
           String cad2 = new String(buffer,"UTF-16");
           System.out.println(cad2);
           
           System.out.println(dis.readUTF());
           
           
           dis.close();
        }
        {
            OutputStream os = new FileOutputStream("salida2.txt");

            Writer w = new OutputStreamWriter(os, "UTF-16");

            w.write("Prueba\r\n");
            w.append("Linea 2").append("\r\n");
            w.append("Linea 3").append("\r\n");
            w.append("Linea 4").append("\r\n");
            w.flush();
            w.close();

            InputStream is = new FileInputStream("salida2.txt");

            Reader r = new InputStreamReader(is, "UTF-16");
            BufferedReader br = new BufferedReader(r);
            String linea = br.readLine();
            System.out.println(linea);
            linea = br.readLine();
            System.out.println(linea);
            
            String cadena = "";
            while((cadena=br.readLine())!=null){
                System.out.println(cadena.toUpperCase());
            }
            
        }
    }
}
